﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using DataBaseTable;

public partial class Manage_Golbal_ChangePassword : System.Web.UI.Page
{
    private static string user = HttpContext.Current.User.Identity.Name;
    private static AdminTable adt = new AdminTable(user);
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        if (!CheckName(AdminName.Text.Trim()))
        {
            return;
        }
        if (!CheckPass(OldPassWord.Text.Trim()))
        {
            return;
        }

        try
        {
            adt.UserPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(NewPassWord.Text.Trim(), "MD5").ToLower();
            adt.Update();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('修改密码成功');</script>");
           
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('修改密码失败');</script>");
        }
    }
    protected void CheckTrueName(object source, ServerValidateEventArgs args)
    {
        if (CheckName(AdminName.Text.Trim()))
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
    private bool CheckName(string name)
    {
        if (adt.TureName == name)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
    protected void CheckPassWord(object source, ServerValidateEventArgs args)
    {
        if (CheckPass(OldPassWord.Text.Trim()))
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
    /// <summary>
    /// 检验密码是否正确
    /// </summary>
    /// <param name="password"></param>
    /// <returns></returns>
    private bool CheckPass(string password)
    {
        if (FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower() == adt.UserPassword.Trim())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}